Ext.define('Monkey.view.code.TemplateController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-codetemplate',

    init: function () {

    },

    reloadTemplate: function () {
        var store = Ext.getCmp('tplGrid').getStore();
        //store.reload({
        //    params: {}
        //});
        store.loadPage(1);
        this.getReferences().templateName.setValue('');
    },

    //显示新增窗口
    addTemplate: function () {
        var window = Ext.create('Monkey.view.code.TemplateWindow');
        window.setTitle('新增代码模版');
        window.setIcon('resources/icons/16/page_add.png');
        window.getViewModel().data.isAdd = true;
        window.show();
    },

    //显示修改窗口
    modifyTemplate: function (record) {
        console.log(record);
        if (Ext.isEmpty(record.data)) {
            var templateGrid = this.getReferences().templateGrid;
            var records = templateGrid.getSelectionModel().getSelection();
            if (records.length == 0) {
                Ext.Msg.show({
                    title: '提示',
                    message: '请先选中需要修改的模版',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
                return;
            }
            record = records[0];
        }

        var window = Ext.create('Monkey.view.code.TemplateWindow');
        window.setTitle('修改代码模版');
        window.setIcon('resources/icons/16/page_edit.png');

        var viewModel = window.getViewModel();
        viewModel.data.isAdd = false;
        viewModel.setModelData(record.data);

        window.show();

    },

    removeTemplate: function () {
        var me = this;
        var templateGrid = this.getReferences().templateGrid;
        var records = templateGrid.getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.Msg.show({
                title: '提示',
                message: '请先选中需要删除的模版',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }

        Ext.Msg.show({
            title: '请确认',
            message: '确认要删除已选中的模板吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function (buttonId) {
                if (buttonId == 'ok') {
                    var ids = Monkey.Utils.arrayModelToString(records, 'id', ',');
                    me.request(Monkey.URL.SERVER_URL + '/m/template/' + ids + '.json', 'DELETE', null, function (data) {
                        console.log(data);
                        if (data.success) {
                            Ext.Msg.show({
                                title: '提示',
                                message: data.message,
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.INFO
                            });
                            me.reloadTemplate();
                        } else {
                            Ext.Msg.show({
                                title: '提示',
                                message: data.message,
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.ERROR
                            });
                        }
                    });
                }
            },
            icon: Ext.Msg.INFO
        });
    },

    rowdblclick: function (node, record) {
        this.modifyTemplate(record);
    },

    ok: function () {
        var view = this.getView(),
            viewModel = this.getViewModel(),
            method = 'PUT',
            me = this;
        console.log(viewModel.data);
        var form = view.getReferences().form;
        if (!form.isValid()) {
            return;
        }
        if (viewModel.data.isAdd) {
            method = 'POST';
        }
        this.request(Monkey.URL.SERVER_URL + '/m/template.json', method, viewModel.data, function (data) {
            var icon = Ext.Msg.ERROR;
            if (data.success) {
                view.close();
                me.reloadTemplate();
                icon = Ext.Msg.INFO;
            }
            Ext.Msg.show({
                title: '提示',
                message: data.message,
                buttons: Ext.Msg.OK,
                icon: icon
            });
        });

    },

    cancel: function () {
        this.getView().close();
    },

    //查询按钮相应事件
    findTemplate: function () {
        var templateName = this.getReferences().templateName.getValue();
        var store = Ext.getCmp('tplGrid').getStore();
        if (!Ext.isEmpty(templateName)) {
            store.loadPage(1, {
                params: {
                    'search.name_like': templateName
                }
            });
        } else {
            store.loadPage(1);
        }
    },

    showUploadPic: function (grid, rowIndex, colIndex) {
        var window = Ext.create('Monkey.view.code.PicUploadWindow');
        window.show();
        var rec = grid.getStore().getAt(rowIndex);
        var templateId = window.getReferences().templateId,
            prevImg = window.getReferences().prevImg;
        templateId.setValue(rec.data.id);
        prevImg.setSrc(Monkey.URL.RESOURCE_FULL_HOST + rec.data.picture);
    },

    /**
     *用户选择了图片后直接上传，绑定了file的change事件
     * @param cmp
     * @param value
     * @returns {*}
     */
    uploadPicture: function (cmp, value) {
        console.log(value);
        var form = cmp.up('form').getForm();
        var me = this,
            references = me.getReferences(),
            picChanged = references.picChanged,
            pathCmp = references.path;
        if (form.isValid()) {
            form.submit({
                url: Monkey.URL.SERVER_URL + '/m/template/pic/upload.json',
                waitMsg: '正在上传，请稍候...',
                success: function (fp, o) {
                    var path = o.result.data.path;
                    var prevImg = me.getReferences().prevImg;
                    prevImg.setSrc(path);
                    pathCmp.setValue(path);
                    picChanged.setValue(true);
                },
                failure: function (form, action) {
                    Ext.Msg.show({
                        title: '提示',
                        message: action.result.message,
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR
                    });
                }
            });
        }
        return value;
    },

    /**
     * 用户预览图片后，点击确定使用图片
     */
    acceptPicture: function () {
        var me = this,
            references = me.getReferences(),
            templateId = references.templateId.getValue(),
            picChanged = references.picChanged.getValue(),
            path = references.path.getValue();
        if (!picChanged) {
            return;
        }
        this.request(Monkey.URL.SERVER_URL + '/m/template/pic/select.json', 'PUT', {
            templateId: templateId,
            path: path
        }, function (data) {
            if (data.success) {
                me.reloadTemplate();
                me.getView().close();
            }
        });
    }

});
